import config from "@/common/config/config.js";  // 配置文件
import storage from "@/common/storage/storage.js"; // 缓存封装
export default {
	domain(){
		return config.uni_app_web_api_url.replace("api","");
	},
	send(options={}){
        // loading加载
        uni.showLoading({
           title: '加载中'
        });
        let headers = {
			// 后端请求头sessionId
			"Cookies":"JSESSIONID="+uni.getStorageSync("sessionId"),
			"Access-Control-Allow-Origin":"*",
		}
        // 拼接路劲，下面的配置文件会提到
		options.url = config.uni_app_web_url + '' + options.url;
        // 请求方式
		options.method = options.method || "GET";
		// options.header=headers
		return new Promise((resolve, reject) =>{
			uni.request(options).then(data=>{
				if(data[1].data.status == 200 ){
					resolve(data[1])
				}else{
					reject(data[1])
				}
				// console.log(data)
			})
		});
	},
	get(url="",dataa={}){
		return this.send({
			url: url,
			data: dataa,
		});
	},
	post(url="",data={}){
			
		return this.send({
			url: url,
			data: data,
			method: "POST"
		});
	},
	isLogin(){
		let sessionId= uni.getStorageSync("sessionId")
		if (sessionId=='' || sessionId==null ){
			return false
		}else{
			return true
		}
	},
	errorMsg(){
		if( ! this.isLogin()){
			console.log("既没有session，也不算登录请求")
			uni.showToast({
				title: '请先登录',
				duration: 2000,
				icon: "error"
			})
			return false
		}else{
			return true
		}
		
	}
};